package com.zhsj.OkeHomeConsumption.ui.activity.citypartner;

import android.app.Dialog;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hjq.base.BaseDialog;
import com.hjq.dialog.DateDialog;
import com.hjq.dialog.MenuDialog;
import com.orhanobut.hawk.Hawk;
import com.zhsj.OkeHomeConsumption.R;
import com.zhsj.OkeHomeConsumption.common.Constants;
import com.zhsj.OkeHomeConsumption.common.MyActivity;
import com.zhsj.OkeHomeConsumption.empty.FormalUserInfo;
import com.zhsj.OkeHomeConsumption.empty.MerchantTypeBean;
import com.zhsj.OkeHomeConsumption.empty.SelectCityEnity;
import com.zhsj.OkeHomeConsumption.ui.activity.citypartner.mvp.AddBusinessInformationPresenter;
import com.zhsj.OkeHomeConsumption.ui.activity.citypartner.mvp.AddBusinessInformationUIInterFace;
import com.zhsj.OkeHomeConsumption.ui.activity.citypartner.mvp.AddEnterprisePresenter;
import com.zhsj.OkeHomeConsumption.ui.activity.citypartner.mvp.AddEnterpriseUIInterFace;
import com.zhsj.OkeHomeConsumption.ui.activity.wallet.ClearEditText;
import com.zhsj.OkeHomeConsumption.utils.SelectCityPicker;
import com.zhsj.OkeHomeConsumption.utils.StringUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.zhsj.OkeHomeConsumption.common.Constants.SHARED_PERFERENCE_USERINFO;


/**
 * 添加装企信息
 */

public class AddEnterpriseActivity extends MyActivity implements AddEnterpriseUIInterFace {


    @BindView(R.id.selet_name)
    ClearEditText seletName;
    @BindView(R.id.selet_city)
    ClearEditText seletCity;
    @BindView(R.id.selet_dizhi)
    ClearEditText seletDizhi;
    @BindView(R.id.selet_fuzeren)
    ClearEditText seletFuzeren;
    @BindView(R.id.selet_perpore)
    ClearEditText seletPerpore;
    @BindView(R.id.selet_phone)
    ClearEditText seletPhone;
    @BindView(R.id.selet_area)
    ClearEditText seletArea;
    @BindView(R.id.selet_type)
    ClearEditText seletType;
    @BindView(R.id.submit_rel)
    RelativeLayout submitRel;
    private ArrayList<SelectCityEnity> mSelectCityLists = null;

    private ArrayList<String> options1Items = new ArrayList<>();//省
    private ArrayList<ArrayList<String>> options2Items = new ArrayList<>();//市
    private ArrayList<ArrayList<ArrayList<String>>> options3Items = new ArrayList<>();//区
    private int proviceId = -1;//省
    private int mcityId = -1;//市
    private int districCountryId = -1;//区
    private int streetId;//街道

    private FormalUserInfo userinfo = null;

    private int categoryLevel1Nameid = -1;


    private AddEnterprisePresenter minePersonPresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_addenterprise;
    }

    @Override
    protected int getTitleId() {
        return R.id.citypartner_title;
    }

    @Override
    protected void initView() {
        userinfo = Hawk.get(Constants.SHARED_PERFERENCE_USERINFO);
        seletCity.setEnabled(true);
        seletCity.setInputType(InputType.TYPE_NULL);
        seletCity.setFocusableInTouchMode(false);
        seletCity.setFocusable(false);
        seletCity.setCursorVisible(false);

        seletType.setEnabled(true);
        seletType.setInputType(InputType.TYPE_NULL);
        seletType.setFocusableInTouchMode(false);
        seletType.setFocusable(false);
        seletType.setCursorVisible(false);

    }

    @Override
    protected void initData() {
        minePersonPresenter = new AddEnterprisePresenter(getActivity(), this);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void UploadPost(String context) {
        try {
            JSONTokener jsonT = new JSONTokener(context);
            JSONObject jsonOBJ = (JSONObject) jsonT.nextValue();
            String code = jsonOBJ.optString("code");
            String message = jsonOBJ.optString("message");
            if (code.equals("N000000")) {
                finish();

            } else if (code.equals(Constants.STATUSTOKENERROR)) {
                // token过期
                if (!message.equals("null")) {
                    toast(message);
                }
            } else {
                if (!message.equals("null")) {
                    toast(message);
                }
            }
        } catch (JSONException ex) {
            Log.e("UploadPost", "提交装企信息");
        }
    }


    @Override
    public void CityListPsot(String context) {
        try {
            JSONTokener jsonT = new JSONTokener(context);
            JSONObject jsonOBJ = (JSONObject) jsonT.nextValue();
            String code = jsonOBJ.optString("code");
            String message = jsonOBJ.optString("message");
            if (code.equals("N000000")) {
                JSONArray jsonarr = jsonOBJ.optJSONArray("data");
                // 数据对接正常
                mSelectCityLists = new Gson().fromJson(jsonarr.toString(), new
                        TypeToken<ArrayList<SelectCityEnity>>() {
                        }.getType());
                getSelectCity(mSelectCityLists);
            } else if (code.equals(Constants.STATUSTOKENERROR)) {
                // token过期
                if (!message.equals("null")) {
                    toast(message);
                }
            } else {
                if (!message.equals("null")) {
                    toast(message);
                }
            }
        } catch (JSONException ex) {
            Log.e("CityListPsot", "获取城市列表");
        }
    }


    /**
     * 选择城市
     *
     * @param mSelectProviceEnities
     */
    private void getSelectCity(ArrayList<SelectCityEnity> mSelectProviceEnities) {
        SelectCityPicker selectCityPicker = new SelectCityPicker(getActivity(), mSelectProviceEnities);
        selectCityPicker.setHalfScreen(true);
        selectCityPicker.setCancelTextColor(R.color.colorAccent);
        selectCityPicker.setSubmitTextColor(R.color.colorAccent);
        selectCityPicker.setOnCitySelectListener(new SelectCityPicker.OnCitySelectListener() {
            @Override
            public void onCitySelect(String province, String city, String county, String town, int provinceId, int cityId, int countyId, int townId) {
                if (!province.equals("") && city.equals("") && county.equals("") && town.equals("")) {
                    seletCity.setText(province);
                } else if (!province.equals("") && !city.equals("") && !county.equals("") && town.equals("")) {
                    seletCity.setText(province + "-" + city + "-" + county);
                } else if (!province.equals("") && !city.equals("") && county.equals("") && town.equals("")) {
                    seletCity.setText(province + "-" + city);
                } else {
                    seletCity.setText(province + "-" + city + "-" + county + "-" + town);
                }
                proviceId = provinceId;
                mcityId = cityId;
                districCountryId = countyId;
                streetId = townId;
            }
        });
        selectCityPicker.show();
    }

    @OnClick({R.id.selet_type, R.id.selet_city, R.id.submit_rel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.selet_type:
                //时间
                new DateDialog.Builder(this)
                        .setTitle("请选择日期")
                        .setListener(new DateDialog.OnListener() {
                            @Override
                            public void onSelected(Dialog dialog, int year, int month, int day) {
                                seletType.setText(year + "年" + month + "月" + day + "日");
                            }

                            @Override
                            public void onCancel(Dialog dialog) {
                                toast("取消了");
                            }
                        })
                        .show();
                break;
            case R.id.selet_city:
                minePersonPresenter.CityListPsot();
                break;
            case R.id.submit_rel:
                empty();
                break;
        }
    }

    private void empty() {
        String city = seletCity.getText().toString();
        if (StringUtils.isEmpty(city)) {
            toast("请选择城市");
            return;
        }
        String operatingType_str = seletType.getText().toString();
        if (StringUtils.isEmpty(operatingType_str)) {
            toast("请选择公司成立时间");
            return;
        }
        String merchantName_str = seletName.getText().toString();
        if (StringUtils.isEmpty(merchantName_str)) {
            toast("请填写公司名称");
            return;
        }
        String address_str = seletDizhi.getText().toString();
        if (StringUtils.isEmpty(address_str)) {
            toast("请填写公司地址");
            return;
        }
        String seletPerpore_str = seletPerpore.getText().toString();
        if (StringUtils.isEmpty(seletPerpore_str)) {
            toast("请填写联系人");
            return;
        }

        String principal_str = seletFuzeren.getText().toString();
        if (StringUtils.isEmpty(principal_str)) {
            toast("请填写公司法定代表");
            return;
        }
        String phone_str = seletPhone.getText().toString();
        if (StringUtils.isEmpty(phone_str)) {
            toast("请填写手机号");
            return;
        }

        String seletArea_str = seletArea.getText().toString();
        if (StringUtils.isEmpty(seletArea_str)) {
            toast("请填写店铺面积");
            return;
        }
        Gson gson = new Gson();
        Map<String, Object> mapjson = new HashMap<>();
        mapjson.put("province", proviceId);
        mapjson.put("conuty", districCountryId);
        mapjson.put("provinceName", "");
        mapjson.put("conutyName", city);
        mapjson.put("companyName", merchantName_str);
        mapjson.put("city", mcityId);

        mapjson.put("address", address_str);

        mapjson.put("legalPerson", principal_str);

        mapjson.put("companyContacts", seletPerpore_str);

        mapjson.put("mobile", phone_str);

        mapjson.put("aera", seletArea_str);

        mapjson.put("companyCreateTime", operatingType_str);
        mapjson.put("partnership", 5);

        String lifeCityCopartnerDTO = gson.toJson(mapjson);//map转json字符串
        minePersonPresenter.UploadPost(lifeCityCopartnerDTO);
    }


}
